<?php namespace Phpcmf\Controllers\Member;


class Home extends \Phpcmf\Table
{
    public $module;
    public $my_module;

    /**
     * 打赏
     */
    public function index() {


        $this->module = \Phpcmf\Service::L('cache')->get('module-'.SITE_ID.'-content');
        if (!$this->module) {
            $this->_msg(0, dr_lang('系统还没有可用的内容模块'));
            exit;
        }
        foreach ($this->module as $dir => $t) {
            if (!in_array('donation', \Phpcmf\Service::M('table')->get_cache_field(SITE_ID.'_'.$dir)) ) {
                unset($this->module[$dir]);
            }
        }
        if (!$this->module) {
            $this->_msg(0, dr_lang('内容模块都没有安装打赏插件'));
            exit;
        }

        $dir = \Phpcmf\Service::L('Input')->get('module');
        if (!$dir || !isset($this->module[$dir])) {
            $one = reset($this->module);
            $this->my_module = $one['dirname'];
        } else {
            $this->my_module = $dir;
        }

        $table = dr_module_table_prefix($this->my_module);

        if (!\Phpcmf\Service::M()->db->tableExists(\Phpcmf\Service::M()->dbprefix($table).'_donation')) {
            $this->_json(0, dr_lang('插件[模块内容打赏]未安装到本模块[%s]', $this->my_module));
        }

        $this->_init([
            'table' => $table,
            'order_by' => 'inputtime desc',
            'where_list' => '`id` IN (select cid from `'.\Phpcmf\Service::M()->dbprefix($table).'_donation` where `uid`='.$this->uid.')',
        ]);

        list($tpl, $data) = $this->_List();


        // 初始化变量
        unset($data['param']['module']);
        unset($data['param']['total']);
        unset($data['param']['order']);

        // 列出内容模块
        foreach ($this->module as $i => $t) {
            $data['param']['module'] = $i;
            $this->module[$i]['url'] =\Phpcmf\Service::L('Router')->member_url('shang/home/'.\Phpcmf\Service::L('Router')->method, $data['param']);
        }

        $list = [];
        if ($data['list']) {
            foreach ($data['list'] as $i => $t) {
                $t['url'] = dr_url_prefix($t['url'], $this->my_module);
                $list[] = $t;
            }
        }

        \Phpcmf\Service::V()->assign([
            'list' => $list,
            'module' => $this->module,
            'my_module' => $this->my_module,
            'del_url' =>\Phpcmf\Service::L('Router')->member_url('shang/home/delete', [
                'module' => $this->my_module,
                'action' =>\Phpcmf\Service::L('Router')->method
            ])
        ]);
        \Phpcmf\Service::V()->display('content_donation.html');
    }


}
